package org.lipert.rccalc.fragments;

import java.text.DecimalFormat;

import org.lipert.rccalc.R;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.actionbarsherlock.app.SherlockFragment;

public class GearRatioFragment extends SherlockFragment implements
		OnClickListener {
	private EditText internalEditText;
	private Button calculateGearRatioButton;
	private EditText pinionGearEditText;
	private EditText spurGearEditText;
	private TextView gearRatioResultTextView;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.gear_ratio_layout, container,
				false);
		pinionGearEditText = (EditText) view.findViewById(R.id.pinionEditText);
		spurGearEditText = (EditText) view.findViewById(R.id.spurEditText);
		internalEditText = (EditText) view.findViewById(R.id.internalEditText);
		gearRatioResultTextView = (TextView) view.findViewById(R.id.gearRatioResultTextView);

		calculateGearRatioButton = (Button) view
				.findViewById(R.id.gearRatioCalculateButton);
		calculateGearRatioButton.setOnClickListener(this);
		return view;
	}

	@Override
	public void onClick(View v) {
		if (v.getId() == calculateGearRatioButton.getId()) {
			Float pinion = Float.parseFloat(pinionGearEditText.getText().toString());
			Float spur = Float.parseFloat(spurGearEditText.getText().toString());
			Float internal = Float.parseFloat(internalEditText.getText().toString());
			
			Float result = spur / pinion * internal;
			DecimalFormat form = new DecimalFormat("0.000"); 
			gearRatioResultTextView.setText(form.format(result));
		}
	}
}
